﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using Jumbot.Common;
namespace JumbotCms.Extends.Feedback
{
    public partial class _ajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 8;//脚本过期时间
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxBatchOper":
                    ajaxBatchOper();
                    break;
                case "ajaxPluginFeedbackAdd":
                    GetFeedbackAdd();
                    break;
                case "ajaxPluginFeedbackDel":
                    GetFeedbackDel();
                    break;
                case "ajaxList":
                    GetFeedbackList();
                    break;
                case "ajaxPluginFeedbackTopList":
                    GetFeedbackTopList();
                    break;
                default:
                    DefaultResponse();
                    break;
            }

            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = "{result :\"未知操作\"}";
        }
        private void ajaxGetList()
        {
            Admin_Load("0000", "json");
            string classid = Str2Str(q("classid"));
            int PSize = Str2Int(q("pagesize"), 0) == 0 ? 15 : Str2Int(q("pagesize"), 0);
            int page = Int_ThisPage();
            string sqlStr = "";
            int countNum = 0;
            string whereStr = "[ParentId]=0";
            if (classid != "0") whereStr += " AND [ClassId]=" + classid;
            doh.Reset();
            doh.ConditionExpress = whereStr;
            countNum = doh.Count("jcms_extends_feedback");
            sqlStr = Jumbot.Common.staticFunction.GetSql("Id,IP,UserName,AddDate,Title,Content,IsPass", "jcms_extends_feedback", "id", PSize, page, "desc", whereStr);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        /// <summary>
        /// 执行批量操作
        /// </summary>
        /// <param name="oper"></param>
        /// <param name="ids"></param>
        private void ajaxBatchOper()
        {
            Admin_Load("0000", "json");
            string act = q("act");
            string ids = f("ids");
            string[] idValue;
            idValue = ids.Split(',');
            string ClassId = string.Empty;
            if (act == "pass")
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.AddFieldItem("IsPass", 1);
                    doh.Update("jcms_extends_feedback");
                }
            }
            else if (act == "nopass")
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.AddFieldItem("IsPass", 0);
                    doh.Update("jcms_extends_feedback");
                }
            }
            else//均为删除
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.Delete("jcms_extends_feedback");
                    doh.Reset();
                    doh.ConditionExpress = "ParentId=" + idValue[i];
                    doh.Delete("jcms_extends_feedback");
                }
            }
            this._response = JsonResult(1, "操作成功");
        }
        private void GetFeedbackAdd()
        {
            string uId, uName, pId;
            pId = f("parentid");
            string _code = f("code");
            int _GuestPost = Jumbot.Common.Validator.StrToInt(Jumbot.Common.XmlCOM.ReadConfig(site.Dir + "_data/config/extends_feedback", "GuestPost"), 0);
            int _NeedCheck = Jumbot.Common.Validator.StrToInt(Jumbot.Common.XmlCOM.ReadConfig(site.Dir + "_data/config/extends_feedback", "NeedCheck"), 0);
            int _PostTimer = Jumbot.Common.Validator.StrToInt(Jumbot.Common.XmlCOM.ReadConfig(site.Dir + "_data/config/extends_feedback", "PostTimer"), 0);
            int _State;
            if (Cookie.GetValue(site.CookiePrev + "user") == null)//判断游客权限
            {
                if (_GuestPost == 0)
                {
                    this._response = "请登录后再进行留言";
                    return;
                }
            }
            if (Cookie.GetValue("ValidateCode") != _code)
            {
                this._response = "验证码错误";
                return;
            }
            bool IsMaster = false;
            if (Cookie.GetValue(site.CookiePrev + "admin") != null)
            {
                uId = Cookie.GetValue(site.CookiePrev + "admin", "id");
                uName = Cookie.GetValue(site.CookiePrev + "admin", "name");
                _State = 1;
                IsMaster = true;
            }
            else
            {
                if (pId != "0")
                {
                    this._response = "管理员登录后才可回复留言!";
                    return;
                }
                if (Cookie.GetValue(site.CookiePrev + "user") != null)
                {
                    uId = Cookie.GetValue(site.CookiePrev + "user", "id");
                    uName = Cookie.GetValue(site.CookiePrev + "user", "name");
                    _State = (_NeedCheck == 0) ? 1 : 0;
                }
                else
                {
                    uId = "0";
                    uName = Jumbot.Common.Strings.HtmlEncode(f("name"));
                    _State = (_NeedCheck == 0) ? 1 : 0;
                }
            }
            if (IsMaster == false)
            {
                int countNum = 0;
                string whereStr = "[ParentId]=0 AND [IP]='" + GetUserIp() + "'";
                if (DBType == "0")
                    whereStr += " and datediff('s',adddate,'" + DateTime.Now.ToString() + "')<" + _PostTimer;
                else
                    whereStr += " and datediff(s,adddate,'" + DateTime.Now.ToString() + "')<" + _PostTimer;
                doh.Reset();
                doh.ConditionExpress = whereStr;
                countNum = doh.Count("jcms_extends_feedback");
                if (countNum > 0)//说明周期内留过言
                {
                    this._response = _PostTimer + "秒内只能发表一次!";
                    return;
                }
            }
            doh.Reset();
            doh.AddFieldItem("ParentId", pId);
            doh.AddFieldItem("AddDate", DateTime.Now.ToString());
            doh.AddFieldItem("Title", Jumbot.Common.Strings.HtmlEncode(f("title")));
            doh.AddFieldItem("Content", GetCutString(Jumbot.Common.Strings.HtmlEncode(f("content")), 200));
            doh.AddFieldItem("IP", GetUserIp());
            doh.AddFieldItem("UserId", uId);
            doh.AddFieldItem("UserName", uName);
            doh.AddFieldItem("IsPass", _State);
            doh.AddFieldItem("ClassId", f("classid"));
            doh.Insert("jcms_extends_feedback");
            this._response = "ok";
        }
        private void GetFeedbackDel()
        {
            string feedbackid = Str2Str(f("feedbackid"));
            if (Cookie.GetValue(site.CookiePrev + "admin") == null)
            {
                this._response = "管理员登录后才可删除";
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "id=" + feedbackid;
                if (doh.Delete("jcms_extends_feedback") == 1)
                {
                    doh.Reset();
                    doh.ConditionExpress = "parentid=" + feedbackid;
                    doh.Delete("jcms_extends_feedback");
                    this._response = "ok";
                }
                else
                    this._response = "id" + feedbackid + "有错误";
            }
        }
        private void GetFeedbackList()
        {
            int PSize = Str2Int(q("pagesize"), 0) == 0 ? 10 : Str2Int(q("pagesize"), 0);
            int page = Str2Int(q("page"), 1);
            string classid = Str2Str(q("classid"));
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[Id]=B.ParentId";
            string whereStr1 = "A.[ParentId]=0 AND A.[ClassId]=" + classid;//外围条件(带A.)
            string whereStr2 = "[ParentId]=0 AND [ClassId]=" + classid;//分页条件(不带A.)
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_extends_feedback");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.Id as Id,A.IP as ip,A.UserId as UserId,A.UserName as UserName,A.AddDate as AddDate,A.Title as Title, A.Content as Content,B.UserId as ReplyUserId,B.UserName as ReplyUserName,B.AddDate as ReplyAddDate,B.Content as ReplyContent", "jcms_extends_feedback", "jcms_extends_feedback", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void GetFeedbackTopList()
        {
            int PSize = Str2Int(q("pagesize"), 0) == 0 ? 10 : Str2Int(q("pagesize"), 0);
            int page = Str2Int(q("page"), 1);
            string classid = Str2Str(q("classid"));
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[Id]=B.ParentId";
            string whereStr1 = "A.[ParentId]=0 AND A.[ClassId]=" + classid;//外围条件(带A.)
            string whereStr2 = "[ParentId]=0 AND [ClassId]=" + classid;//分页条件(不带A.)
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_extends_feedback");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.Id as Id,A.IP as ip,A.UserId as UserId,A.UserName as UserName,A.AddDate as AddDate,A.Title as Title, A.Content as Content,B.UserId as ReplyUserId,B.UserName as ReplyUserName,B.AddDate as ReplyAddDate,B.Content as ReplyContent", "jcms_extends_feedback", "jcms_extends_feedback", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dtFeedback = doh.GetDataTable();
            string ResponseStr = "<ul>";
            for (int j = 0; j < dtFeedback.Rows.Count; j++)
            {
                ResponseStr += "<li><a href=\"" + site.Dir + "extends/feedback/index.aspx?classid=" + classid + "\" target=\"_blank\">" + dtFeedback.Rows[j]["Title"].ToString() + "</a></li>";
            }
            ResponseStr += "</ul>";
            dtFeedback.Clear();
            dtFeedback.Dispose();
            this._response = ResponseStr;
        }
    }
}
